<template>
	<view class="page">
		<u-navbar title="积分项" border @leftClick="$utils.back()" placeholder />
		<view class="body">
			<z-paging ref="paging" style="height: 100%;" :fixed="false" v-model="data" @query="query"
				:default-page-no="1" :default-page-size="10">
				<template v-if="loading">
					<view style="height: 30rpx;width: 100%;margin-top: 20rpx;" v-for="item in 30" :key="item"
						class="Skeleton" />
				</template>
				<template v-else>
					<view v-for="item in data" :key="item.id" class="userBox">
						<u-text :lines='1' :text='item.title' />
						<view style="margin-left: 20rpx;">
							{{item.point}}分
						</view>
						<view @click.stop="()=>{}" style="margin-left: 20rpx;">
							<u-icon name="more-dot-fill" size="50rpx" @click='openSelect(item)'></u-icon>
						</view>
					</view>
				</template>
			</z-paging>
		</view>
		<view class="add viewWidth" v-if="!loading" @click="jumpAdd">新增积分
		</view>
		<root-portal>
			<u-action-sheet :actions="columns" :show="selectShow" @close='selectShow=false' @select="confirm"
				closeOnClickOverlay></u-action-sheet>
			<u-modal :show="delShow" showCancelButton @confirm='del()' @cancel='delShow=false'>
				<view style="padding: 12px 25px 25px 25px;font-size: 30rpx;">
					是否确认删除
				</view>
			</u-modal>
			<u-modal :show="!(!modalShow)" :showCancelButton='true' @close='cancel' @cancel='cancel'
				@confirm='modalConfirm'>
				<view style="width: 100%;">
					<view class="popupTitle">
						<u-icon name='close' color='#fff' />
						<view>
							{{modalShow}}积分项
						</view>
						<u-icon name='close' @click='cancel' />
					</view>
					<view style="padding: 0 40rpx;">
						<u--form labelWidth='100' labelPosition="top" :model="form" :rules="rules" ref="uForm">
							<u-form-item label="标题:" prop="title" borderBottom>
								<u--input v-model="form.title" placeholder="请输入标题" />
							</u-form-item>
							<u-form-item label="积分:" prop="point" borderBottom>
								<u--input type='number' v-model="form.point" placeholder="请输入积分" />
							</u-form-item>
						</u--form>
					</view>
				</view>
			</u-modal>
		</root-portal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					point: null,
					title: null
				},
				rules: {
					'point': [{
						required: true,
						message: '请输入积分',
						trigger: ['blur', 'change']
					}, {
						validator: (rule, value, callback) => {
							return value > 0
						},
						message: '积分必须大于0',
						trigger: ['blur', 'change']
					}],
					'title': {
						type: 'string',
						required: true,
						message: '请输入标题',
						trigger: ['blur', 'change']
					},
				},
				modalShow: null,
				disabled: false,
				delShow: false,
				selectItem: {},
				selectShow: false,
				data: [],
				loading: true,
			}
		},
		computed: {
			columns() {
				return [{
					name: '编辑'
				}, {
					name: '删除'
				}, {
					name: '退出'
				}, ]
			}
		},
		methods: {
			modalConfirm() {
				if (this.modalShow == '新增') {
					this.disabled = true
					this.$api.My.Point.Add(this.form).then(res => {
						uni.$u.toast('新增积分项成功')
						this.disabled = false
						this.cancel()
						this.$refs.paging.reload()
					}).catch(err => {
						this.disabled = false
						uni.$u.toast(err.data.message)
					})
				} else if (this.modalShow == '编辑') {
					this.disabled = true
					this.$api.My.Point.Update(this.selectItem.id, this.form).then(res => {
						uni.$u.toast('编辑积分项成功')
						this.disabled = false
						this.cancel()
						this.$refs.paging.reload()
					}).catch(err => {
						this.disabled = false
						uni.$u.toast(err.data.message)
					})
				}
			},
			cancel() {
				if (this.disabled) return
				this.modalShow = false
				this.$refs.uForm.resetFields()
				this.form = {
					point: null,
					title: null
				}
				this.modalShow = false
			},
			jumpAdd() {
				this.modalShow = '新增'
				setTimeout(() => {
					this.$refs.uForm.setRules(this.rules)
				}, 500)
			},
			del() {
				this.delShow = false
				if (this.disabled) return
				this.disabled = true
				uni.showLoading({
					title: '删除中...'
				})
				this.$api.My.Point.Del(this.selectItem.id).then(err => {
					this.data = this.data.filter(item => item.id != this.selectItem.id)
					this.disabled = false
					uni.hideLoading()
				}).catch(err => {
					uni.hideLoading()
					uni.$u.toast(err.data.message)
					this.disabled = false
				})
			},
			confirm(e) {
				this.selectShow = false
				if (e.name == '编辑') {
					this.modalShow = '编辑'
					this.form.title = this.selectItem.title
					this.form.point = this.selectItem.point
					setTimeout(() => {
						this.$refs.uForm.setRules(this.rules)
					}, 500)
				} else if (e.name == '删除') {

					this.delShow = true
				} else if (e.name == '退出') {

					this.selectShow = true
				}
			},
			openSelect(item) {
				this.selectShow = true
				this.selectItem = item
			},
			query(page, size) {
				let obj = {
					page,
					size
				}
				uni.showLoading({
					title: '加载中...'
				})
				this.$api.My.Point.List(obj).then(res => {
					uni.hideLoading()
					this.$refs.paging.complete(res.data.points.data)
					this.loading = false
				}).catch(err => {
					uni.hideLoading()
					uni.$u.toast(err.data.message)
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.add {
		border-radius: 100rpx;
		background-color: #ddd;
		text-align: center;
		padding: 20rpx;
		box-sizing: border-box;
	}

	.page {
		padding: 0rpx 20rpx;
		height: 100vh;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 10rpx);
		/* 兼容 iOS 设备 */
		padding-bottom: calc(env(safe-area-inset-bottom) + 10rpx);
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
	}

	.body {
		flex: 1;
		overflow: hidden;
	}

	.userBox {
		display: flex;
		align-items: center;
		margin-top: 20rpx;
		background-color: #f5f5f5;
		border-radius: 8rpx;
		padding: 10rpx 20rpx;
	}
</style>